﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Editing.Actions;
using Blueprints.IO;
using Blueprints.Validation;
using System;
using System.Collections.Generic;

namespace Blueprints.Editing
{
    partial class EditNode : INode
    {
        private readonly Dictionary<ulong, string> defaultAvailableValues = new Dictionary<ulong, string>();
        private readonly List<string> defaultAvailableTypes = new List<string>();

        string INode.Content
        {
            get
            {
                return TraceableNode.Content;
            }
        }

        /// <summary>
        /// Gets the value of this node.
        /// </summary>
        public string Value
        {
            get
            {
                IValidatedNode validation;
                if (TryGetValidatedNode(out validation))
                {
                    return validation.Value;
                }

                int nodeIndex = EditContext.Nodes.IndexOfContentNode(this);
                if (nodeIndex < 0)
                {
                    return null;
                }

                return EditContext.Nodes[nodeIndex].TraceableNode.Content;
            }
            set
            {
                if (Value == value)
                {
                    return;
                }

                EditContext.Do(new ChangeValueAction(this, value));
            }
        }

        /// <summary>
        /// Gets the id of this node.
        /// </summary>
        public string Id
        {
            get
            {
                return TraceableNode.Id;
            }
            set
            {
                if (Id == value)
                {
                    return;
                }

                if (!StartNode.IsValidName(value))
                {
                    throw new InvalidOperationException();
                }

                EditContext.Do(new ChangeIdAction(this, value));
            }
        }

        /// <summary>
        /// Gets the uri of the node to which this node links.
        /// </summary>
        public string Link
        {
            get
            {
                return TraceableNode.Link;
            }
            set
            {
                if (Link == value)
                {
                    return;
                }

                EditContext.Do(new ChangeLinkAction(this, value));
            }
        }

        /// <summary>
        /// Gets the name of this node.
        /// </summary>
        public string Name
        {
            get
            {
                return TraceableNode.Name;
            }
            set
            {
                if (Name == value)
                {
                    return;
                }

                if (!StartNode.IsValidName(value))
                {
                    throw new InvalidOperationException();
                }

                EditContext.Do(new ChangeNameAction(this, value));
            }
        }

        NodeType INode.NodeType
        {
            get
            {
                return TraceableNode.NodeType;
            }
        }

        string INode.Type
        {
            get
            {
                return TraceableNode.Type;
            }
        }

        /// <summary>
        /// Gets the type of node.
        /// </summary>
        public string Type
        {
            get
            {
                IValidatedNode validation;
                if (TryGetValidatedNode(out validation))
                {
                    return validation.Type;
                }

                return TraceableNode.Type;
            }
            set
            {
                if (Type == value)
                {
                    return;
                }

                EditContext.Do(new ChangeTypeAction(this, value));
            }
        }

        /// <summary>
        /// Gets the path in the document to this node.
        /// </summary>
        public string Path
        {
            get
            {
                IValidatedNode validation;
                if (!TryGetValidatedNode(out validation))
                {
                    return null;
                }

                return validation.Path;
            }
        }

        /// <summary>
        /// Gets the deserialization result for this node.
        /// </summary>
        public DeserializationResult Result
        {
            get
            {
                IValidatedNode validation;
                if (TryGetValidatedNode(out validation))
                {
                    return validation.Result;
                }

                return DeserializationResult.Unknown;
            }
        }

        /// <summary>
        /// Gets an indication of how this node should be edited.
        /// </summary>
        public EditType EditType
        {
            get
            {
                IValidatedNode validation;
                if (TryGetValidatedNode(out validation))
                {
                    return validation.EditType;
                }

                return EditType.Unknown;
            }
        }

        /// <summary>
        /// Gets the types available for this node.
        /// </summary>
        public IReadOnlyList<string> AvailableTypes
        {
            get
            {
                IValidatedNode validation;
                if (TryGetValidatedNode(out validation))
                {
                    return validation.AvailableTypes;
                }

                return defaultAvailableTypes;
            }
        }

        /// <summary>
        /// Gets the available values for this node, if this is a value node.
        /// </summary>
        public IReadOnlyDictionary<ulong, string> AvailableValues
        {
            get
            {
                IValidatedNode validation;
                if (TryGetValidatedNode(out validation))
                {
                    return validation.AvailableValues;
                }

                return defaultAvailableValues;
            }
        }

        /// <summary>
        /// Gets the properties of this node, if this is an object node.
        /// </summary>
        public IReadOnlyDictionary<string, EditProperty> Properties
        {
            get
            {
                return properties;
            }
        }

        /// <summary>
        /// Gets the elements of this node, if this is a collection node.
        /// </summary>
        public ElementCollection Elements
        {
            get;
            private set;
        }

        /// <summary>
        /// Removes the node from the blueprint
        /// </summary>
        /// <returns>True if the node was removed, false if the node was not part of the blueprint.</returns>
        public bool Remove()
        {
            EditNode parentNode = this.GetParentNode();
            if (parentNode == null)
            {
                return false;
            }

            EditContext.Do(new RemoveNodeAction(this, parentNode));
            return true;
        }
    }
}